<?php //file: _bank.inc

require_once "./_common.inc";

/*
 * Function to verify the money orders
 */
function verify_MO($MOs)
{
	
	echo '<table width="800" border="1" cellspacing="1" cellpadding="3" align=center>';
	echo '<tr><td colspan=3 align=center>Unblinded Money Order List</td></tr>';
	echo '<tr><td>#</td><td>Uniqueness String</td><td>Amount</td></tr>';
	
  $mo1 = mo_string_to_obj(current($MOs));
  $uq_array = array();  //list of uniqueness string
  $amount = $mo1['amount'];
  //$id_hash = $mo['id_hash'];	
  foreach ($MOs as $n=>$mo){
  	$mo = mo_string_to_obj($mo);	
	// verify that the amount of each money order is the same 
  	if ($mo['amount']!=$amount) {
  		echo "<tr><td>$n</td><td>$mo[uniquestring]</td><td class='wrong'>$mo[amount]</td></tr>";
  		echo "</table>";
  		echo "The money order amount is differnt than others.\n";
  		return FALSE;
  	}
  // verify that each uniqueness string is different 
  	if (array_search($mo['uniquestring'], $uq_array)) {
  		echo "<tr><td>$n</td><td class='wrong'>$mo[uniquestring]</td><td>$mo[amount]</td></tr>";
  		echo "</table>";
  		echo "The uniqueness string is repeated.\n";
  		return FALSE;
  	}
  	$uq_array[] =  $mo['uniquestring'];	//add one element
  	echo "<tr><td>$n</td><td>$mo[uniquestring]</td><td>$mo[amount]</td></tr>";
  	/*
  	// verify that each ith ID string is the same
	for($i = 0; $i < sizeof($id_hash); $i++) //N pairs of ID strings
		if (array_diff($id_hash[$i], $mo['id_hash'][$i])){
 			echo "</table>";
			echo "The ID string in this money order is differnt than in other money order.\n";
	  		return FALSE;
			}
			*/
  }
  echo "</table>";
  return TRUE;
}
/*
 * Function to verify revealed pairs ID strings to form same ID strings
 */
function verify_ID_strings($IDstrs, $unblinded)
{
/*
 * 
 *	$indexes = array_keys($ID_Ls);
	$ind2 = array_keys($ID_Ls[$indexes[0]]);
	
	//get the first ID string
	$IDstr = join_id_string($ID_Ls[$indexes[0]][$ind2[0]], $ID_Rs[$indexes[0]][$ind2[0]]);
	
	//get ID strings for each money order
	for ($i=1; $i<sizeof($indexes); $i++){
		// get n pairs of ID strings for each money order
		for ($j=0; $j<sizeof($ind2); $j++)
				//combine left half and right half
			if (!strcmp($IDstr, join_id_string($ID_Ls[$indexes[$i]][$ind2[$j]], $ID_Rs[$indexes[$i]]))){
				echo "You are cheating!\n The ID strings are not all the same.\n";
				return FALSE;
		}
	}
	*/
	
	$IDpairs = current($IDstrs);
	$ID = current($IDpairs);
	$IDstr = join_id_string($ID[0],$ID[1]); //get first ID string as standard
	//echo "$IDstr</br>";
	foreach ($IDstrs as $IDpairs) 
		foreach ($IDpairs as $ID)
			if (strcmp($IDstr, join_id_string($ID[0], $ID[1]))<>0){
				echo "You are cheating!\n The ID strings are not all the same.\n";
				return FALSE;
	}
		
	return TRUE;	
}


/* 
 * 
 */
function bank_verify_signature($signedMO, $amount, $uniqeStr, $conn){
	//form money order string from post data
 	$mo_string = mo_obj_to_string(MO_from_database($amount, $uniqeStr, $conn));
 	return is_valid_signature($mo_string, $signedMO);
}

function blind_sign($blind){

	$signed = rsa_decrypt($blind, PRIVATE_EXP, MODULUS); // original decrypt before Java
	
	return $signed;	
}




/*
 * Function to check if the unblinded money order match blinded money order
 */
function verify_blinded($mo_string, $blinded_MO,$r){
	$h = md5($mo_string);
	$blind = blind($h, PUBLIC_EXP, MODULUS, $r);	
	return ($blind==$blinded_MO);
}

/*
 * Update money in account
 * $option: '+' or '-'
 * $acount: 'm' or 'c'
 */
function update_Money($acount, $amount, $option, $conn){
	
			$sql = "update " .$acount ."_account set BALANCE=BALANCE" .$option .$amount;
			$r = db_execute($conn, $sql);
//			echo "SELECT BALANCE FROM " .$acount ."_account";
			$balance = db_do_query($conn,"SELECT BALANCE FROM " .$acount ."_account");
			
			if ($acount=='m') echo "Merchant-Bob</br>";
			elseif ($acount=='mb') echo "Merchant-Rob</br>";
			else echo "Customer-Alice</br>";
			if ($option=='+') echo ": Your money order valued \$" .$amount ." has been successfully deposited</br>";
			else echo "You have successfully buy this money order valued \$" .$amount ."</br>";
			echo "Your account balance is now: $" .$balance[0][BALANCE] ."</br>";
}


/*
 *	Function to join two ID strings. 
 */
function join_id_string($l, $r){

	$l_arr = str_split($l);
	$r_arr = str_split($r);
	
	for($i = 0; $i < strlen($l)/3; $i++)
		$s[$i] = chr((int)($l_arr[$i*3] . $l_arr[$i*3+ 1] . $l_arr[$i*3+ 2])^ (int)($r_arr[$i*3] . $r_arr[$i*3+ 1] . $r_arr[$i*3+ 2]));

//		print_r($s);
	return implode($s);
}


/*
 * Function to check database to make sure a money order with the same uniqueness string has not been previously deposited. 
 */
function is_cheat($unique_str, $conn){
	//get the unique string from money order
	//check it with database
	$moneyOrders = db_do_query($conn,'SELECT * FROM MO_DETAIL_BANK where UNIQUENESS_STRING=' .$unique_str);
	
	//no found, no cheating
	
	if (sizeof($moneyOrders)==0) return FALSE;
	//if found, compare itendity string
	else return TRUE;
}

/*
 *  Function to compare two selector string to find a bit position where one is different than the other
 */
function ID_index($s1, $s2){
	if ($s1==$s2) return -1;
	$arr1 = str_split($s1);
	$arr2 = str_split($s2);
	for ($i=0; $i<sizeof($arr1); $i++){
		if ($arr1[$i]!=$arr2[$i])
			return $i+1;  //s1 is left half		
	}
}

